from overfish.libs.ModelException import ModelException
u"""
OverFish CMS

Created on: Jul 7, 2011 1:38:25 PM

@author: Tomas Holub (tomas.holub@olc.cz)
@package: OverfishCMS

Base class for all models
u"""

from overfish.libs.BaseObject import BaseObject
from django.conf import settings

class BaseModel(BaseObject):
    u"""
    Base class for all models
    """
    def __init__(self, collection, connection = None, database = None):
        
        if connection == None:
            self.__connection = settings.CONNECTION
        else:
            self.__connection = connection
        
        if database == None:
            self.__database = self.__connection[settings.DATABASE]
        else:
            self.__database = self.__connection[database]
        
        self.__collection = self.__database[collection]
    
    def getDatabase(self):
        return self.__database
    
    def getCollection(self):
        return self.__collection
    
    def getConnection(self):
        return self.__connection
    
    def validate(self, data):
        return True
    
    def save(self, data):
        if self.validate(data) == True:
            return self.__collection.insert(data)
        else:
            raise ModelException(u"Validation of inserted data failed.")
        
        